import Mock from 'mockjs'

const tokens = {
  admin: {
    token: 'admin-token',
    roles: ['admin']
  },
  editor: {
    token: 'editor-token',
    roles: ['editor']
  },
  outer: {
    token: 'outer-token',
    roles: ['outer']
  },
  cashier: {
    token: 'cashier-token',
    roles: ['cashier']
  },
  auditor: {
    token: 'auditor-token',
    roles: ['auditor']
  },
}

const editors =  Mock.mock({
  'list|0-10': [{
    'id|+1': 1,
    'name|+1': ['梨花', '鲁花', '李华', '小敏', '哈哈'],
    account: '@email',
    password: 'belulu@2020'
  }]
})

const bidcos =  Mock.mock({
  'list|0-10': [{
    base: {
      'id|+1': 1,
      username: '@email',
      password: 'belulu@2020',
      name: '@cname',
      tel: /\d{11}/,
      'email|1': ['@email', ''],
      'line': '',
      'wxid|1': ['', 'chengzhem', ''],
      'address|1': ['', '@county(true)'],
      'postcode|1': [/\d{6}/, ''],
      'bankname|1': ['', '中国民生银行'],
      'bankcard|1': ['', '6223 3434 3232 343']
    },
    buy: {
      'id|1': +1,
      subid: 1,
      'buy_level|1': ['@natural(0, 3)', ''],
      'credit_level|1': ['@natural(0, 3)', ''],
      'judg_level|1': ['@natural(0, 3)', ''],
      'fee_rate|1': [0.011],
      remark: '@csentence'
    }
  }],
  'all|1-10': [{
    'id|+1': 1,
    'name|+1': ['易胡代拍', '代拍-@cname'],
  }],
  total: 30
})

const users = {
  'admin-token': {
    roles: ['admin'],
    avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
    name: '管理员', // 子账户用
    username: 'admin',
    max: '@integer(1, 1000)',
    subname: '万通国际株式会社'
  },
  'editor-token': {
    roles: ['editor'],
    avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
    name: '入库员',
    username: 'editor001',
    subname: '万通国际株式会社'
  },
  'outer-token': {
    roles: ['outer'],
    avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
    name: '出库员',
    username: 'outer001',
    subname: '万通国际株式会社'
  },
  'cashier-token': {
    roles: ['cashier'],
    avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
    name: '会计',
    username: 'cashier001',
    subname: '万通国际株式会社'
  },
  'auditor-token': {
    roles: ['auditor'],
    avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
    name: '审核员',
    username: 'auditor001',
    subname: '万通国际株式会社'
  }
}

const logo = {
  logo: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
  intro: '@ctitle(0, 100)'
}

const keys = Mock.mock({
  'list|20': [{
    'id|+1': 1,
    'num|+1': 101,
    key: '@guid',
    is_bind: '@boolean'
  }],
  total: 888
})

export default [
  // user login
  {
    url: '/admin/login',
    type: 'post',
    response: config => {
      const { username } = config.body
      const token = tokens[username]
      // mock error
      if (!token) {
        return {
          code: 403,
          message: 'Account and password are incorrect.'
        }
      }

      return {
        code: 200,
        data: token
      }
    }
  },

  // get user info
  {
    url: '/admin/info\.*',
    type: 'get',
    response: config => {
      const { token } = config.query
      const info = users[token]

      // mock error
      if (!info) {
        return {
          code: 801,
          message: 'Login failed, unable to get user details.'
        }
      }

      return {
        code: 200,
        data: info
      }
    }
  },

  // user logout
  {
    url: '/admin/logout',
    type: 'post',
    response: _ => {
      return {
        code: 200,
        data: 'success'
      }
    }
  },
  {
    url: '/admin/exportkeys',
    type: 'post',
    response: config => {
      return {
        code: 200,
        data: {
          name: 'secret.xls',
          url: 'https://www.glzy8.com/down/?x=/upfiles/zt/excel/b371307a2d56251e.rar&y=zt01',
          max: 500
        },
        msg: '秘钥生成成功，点击下载'
      }
    }
  },
  {
    url: '/admin/downloadkeys',
    type: 'get',
    response: config => {
      return {
        code: 200,
        data: {
          name: 'secret.xls',
          url: 'https://www.glzy8.com/down/?x=/upfiles/zt/excel/b371307a2d56251e.rar&y=zt01',
        }
      }
    }
  },
  {
    url: '/admin/getKeys',
    type: 'get',
    response: config => {
      const { bind } = config.query
      if (bind && bind == '0') {
        keys.list.forEach(el => el.is_bind = false)
      } else {
        keys.list.forEach(el => el.is_bind = '@boolean')
      }
      return {
        code: 200,
        data: keys
      }
    }
  },
  {
    url: '/admin/getSubs',
    type: 'get',
    response: _ => {
      return {
        code: 200,
        data: editors.list
      }
    }
  },
  {
    url: '/admin/updateSub',
    type: 'post',
    response: config => {
      const { id, name, account, password } = config.body
      return {
        code: 200,
        data: { 
          id: id ? Number(id) : 99,
          name, account, password
        },
        msg: id ? '账号修改成功！' : '账号添加成功！'
      }
    }
  },
  {
    url: '/admin/password',
    type: 'post',
    response: _ => {
      return {
        code: 200,
        msg: '修改成功，请用新密码重新登入'
      }
    }
  },
  {
    url: '/admin/getLogo',
    type: 'get',
    response: _ => {
      return {
        code: 200,
        data: logo
      }
    }
  },
  {
    url: '/admin/setLogo',
    type: 'post',
    response: config => {
      return {
        code: 200,
        data: logo,
        msg: '信息设置成功'
      }
    }
  },
  {
    url: '/admin/getBidCos',
    type: 'get',
    response: _ => {
      return {
        code: 200,
        data: bidcos
      }
    }
  },
  {
    url: '/admin/getAllBidCos',
    type: 'get',
    response: _ => {
      return {
        code: 200,
        data: bidcos.all
      }
    }
  },
  {
    url: '/admin/updateBidCo',
    type: 'post',
    response: config => {
      const { id, name, account, password } = config.body
      return {
        code: 200,
        data: { 
          id: id ? Number(id) : 99,
          name, account, password
        },
        msg: id ? '账号修改成功！' : '账号添加成功！'
      }
    }
  },
]
